import { observable, action } from 'mobx'
import axios from '../../../common/request'
import notificationWithIcon from "../../common/notificationWithIcon";
import {message} from "antd/lib/index";

class LoginStore {
    actions = {
        login:action((value,toggle,form)=>{
            axios.post("/api/common/login",value)
                .then((response)=>{
                    toggle(false)
                    if(response.data.status.code===1){
                        if(window.location.search!=""){
                            window.location.href=window.location.search.substring(6,window.location.search.length)
                        }
                        else{
                            window.location.href="/"
                        }
                    }
                    else if(response.data.status.code===1010) {
                        form.setFields({
                            phoneNo: {
                                value: "",
                                errors: [new Error(response.data.status.message)],
                            },
                        });
                        message.error(response.data.status.message, 5)
                    }
                    else if(response.data.status.code===1011){
                        form.setFields({
                            password: {
                                value: "",
                                errors: [new Error(response.data.status.message)],
                            },
                        });
                        message.error(response.data.status.message, 5)
                    }
                })
                .catch(function (error) {
                    toggle(false)
                    notificationWithIcon('error','登陆失败',error.response)
                    console.log(error);
                });
        })
    }
}
export default new LoginStore()